package com.lagou.task13;

public class StringBuilderTest {
    public static void main(String[] args) {
        StringBuilder strb1 = new StringBuilder();
        System.out.println(strb1.capacity());//16
        System.out.println("---------------------------");
        StringBuilder strb2 = new StringBuilder(20);
        System.out.println(strb2.capacity());//20
        System.out.println(strb2.length());//0

        System.out.println("---------------------------");
        StringBuilder strb3 = new StringBuilder("hello");
        System.out.println(strb3.capacity());//21
        System.out.println(strb3.length());//5

        System.out.println("---------------------------");
        strb3.insert(0,123);
        System.out.println("strb3: " + strb3);//strb3: 123hello
        System.out.println(strb3.capacity());//21
        System.out.println(strb3.length());//8
        strb3.append("**");
        System.out.println("strb3: " + strb3);//strb3: 123hello**
        System.out.println(strb3.capacity());//21
        System.out.println(strb3.length());//10
        System.out.println("---------------------------");
        //返回上一个界面command +alt + <-
        //超出自动扩容，新容量 = 原始容量*2+2
        strb3.append("123456789100");
        System.out.println(strb3.capacity());//44
        System.out.println(strb3.length());//22


        System.out.println("---------------------------");
        //字符串中字符的删除
        System.out.println(strb3);//123hello**123456789100
        strb3.deleteCharAt(8);
        System.out.println(strb3);//123hello*123456789100
        strb3.delete(3,8);
        System.out.println(strb3);//123*123456789100

        System.out.println("---------------------------");
        //实现字符串内容的修改
        StringBuilder sb1 = new StringBuilder("liyulin");
        sb1.setCharAt(6,'m');
        System.out.println(sb1);//liyulim
        sb1.replace(4,8,"tian");
        System.out.println(sb1);
        sb1.reverse();
        System.out.println(sb1);

        System.out.println("---------------------------");
        //笔试考点
        //考点一，既然可以直接修改字符串，为什么还有返回值？ 答：实现连续调用
        //考点2，StringBuilder和String类型的转换
        StringBuilder  sb2 =  new StringBuilder("abc");
        String str2 = "efg";

        String str3 = sb2.toString();
        StringBuilder sb3 = new StringBuilder(str2);

        //StringBuffer > String > StringBuilder   效率

    }
}
